Ext.define('Ext.ux.GridToolbar', {
    extend: 'Ext.toolbar.Toolbar',
    xtype: 'gridtoolbar',
    initComponent: function () {
        this.items = [
            {
                text: '显示',
                glyph: 0xf022,
                handler: this.showRecord
            }, {
                text: '新建',
                glyph: 0xf016,
                handler: this.newRecord
            }, {
                text: '复制',
                glyph: 0xf0c5,
                handler: this.copyRecord
            }, {
                text: '编辑',
                glyph: 0xf044,
                handler: this.editRecord
            }, {
                text: '删除',
                glyph: 0xf014,
                disabled: true,
                itemId: 'delete',
                handler: this.deleteRecord
            }, {
                text: '保存',
                glyph: 0xf0c7,
                handler: this.saveRecord
            }, {
                text: '导入',
                glyph: 0xf062,
                handler: this.importRecord
            }, {
                text: '导出',
                glyph: 0xf02e,
                handler: this.exportRecord
            }, {
                text: '打印',
                glyph: 0xf02f,
                handler: this.printRecord
            }, {
                text: '生成人工规则',
                glyph: 0xf02f,
                handler: this.generateManual
            }
        ];
        return this.callParent();
    },
    newRecord: function () {
        var grid, model, store;
        grid = this.up('grid');
        store = grid.getStore();
        model = Ext.create(store.model);
        return store.insert(0, model);
    },
    copyRecord: function () {
        var grid, model, sm, store;
        grid = this.up('grid');
        store = grid.getStore();
        model = Ext.create(store.model);
        sm = grid.getSelectionModel();
        if (sm.getSelection().length !== 1) {
            Ext.toast({
                title: '警告',
                html: '请先选择一条记录, 然后再执行此操作!',
                align: 'tr',
                useXAxis: true,
                slideInDuration: 500
            });
            return;
        }
        Ext.Array.each(model.fields, function (field) {
            return model.set(field.name, sm.getSelection()[0].get(field.name));
        });
        model.set('Alias', null);
        sm.select(model);
        return store.insert(0, model);
    },
    editRecord: function () {
        var grid, sm;
        grid = this.up('grid');
        sm = grid.getSelectionModel();
        return sm.getSelection()[0].beginEdit(0, 0);
    },
    deleteRecord: function () {
        var grid, msg, record, records, sm, _i, _len;
        grid = this.up('grid');
        sm = grid.getSelectionModel();
        records = sm.getSelection();
        if (!(records.length >= 1)) {
            Ext.toast({
                title: '警告',
                html: '请至少选择一条记录, 然后再执行此操作!',
                align: 'tr',
                useXAxis: true,
                slideInDuration: 500
            });
            return;
        }
        msg = '';
        for (_i = 0, _len = records.length; _i < _len; _i++) {
            record = records[_i];
            msg += "<li>" + (record.getNameValue()) + "</li>";
        }
        return Ext.Msg.confirm('确认框', "你确定要删除以下 " + records.length + " 条记录吗?<ol>" + msg + "</ol>", function (btn) {
            var store;
            if (btn === 'yes') {
                store = grid.getStore();
                return store.remove(records);
            }
        });
    },
    saveRecord: function () {
        var grid, store;
        grid = this.up('grid');
        store = grid.getStore();
        return store.sync({
            success: function () {
                return Ext.Msg.alert("确认框", "保存成功!");
            },
            failure: function () {
                return Ext.Msg.alert("确认框", "保存失败!");
            }
        });
    },
    importRecord: function () {
        return Ext.create('Ext.window.Window', {
            title: '导入 Excel',
            width: 500,
            height: 300,
            modal: true,
            items: [
                {
                    xtype: 'form',
                    bodyPadding: 10,
                    layout: 'anchor',
                    defaults: {
                        anchor: '100%'
                    },
                    items: [
                        {
                            xtype: 'component',
                            html: '选择 Excel 文件'
                        }, {
                            xtype: 'combo',
                            name: 'genre',
                            fieldLabel: '栏目',
                            store: [[1, '热剧'], [2, '少儿']]
                        }, {
                            xtype: 'filefield',
                            name: 'file',
                            emptyText: '请选择本地文件',
                            buttonText: '查找'
                        }
                    ],
                    buttons: [
                        {
                            text: '上传',
                            handler: 'fileUpload'
                        }
                    ]
                }
            ]
        }).show();
    },
    generateManual: function () {
        Ext.Ajax.request({
            url: '/api/model_manuals/update',
            waitMsg: '正在生成人工规则，请耐心等候...',
            success: function () {
                return Ext.Msg.alert('确认框', '生成规则成功!');
            },
            failure: function () {
                return Ext.Msg.alert('确认框', '生成规则失败!');
            }
        });
    }
});
